package com.quanyan.ic.vendingMachine.protocol.codec.protobuf;

import com.quanyan.ic.vendingMachine.protocol.Cmd;
import com.quanyan.ic.vendingMachine.protocol.Code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/7/28.
 */
public class VmMessageBuilder {

    private VmMessageProto.VmMessage.Builder builder;
    private String vmNumber;

    public VmMessageBuilder(String vmNumber) {
        if (this.builder == null) {
            this.builder = VmMessageProto.VmMessage.newBuilder();
        }
        this.vmNumber = vmNumber;
        builder.setVmNumber(vmNumber);
    }

    public VmMessageBuilder buildCmd(Cmd cmd) {
        builder.setCmd(cmd.getValue());
        return this;
    }

    public VmMessageBuilder buildLongTime(String longtime){
        builder.setLogtime(longtime);
        return  this;
    }

    public VmMessageBuilder buildEquipmentId(String equipmentId) {
        builder.setEquipmentId(equipmentId);
        return this;
    }
    public VmMessageBuilder buildUpgradeFileType(String fileType) {
        builder.setUpgradeFileType(fileType);
        return this;
    }

    public VmMessageBuilder buildRespCode(Code code) {
        builder.setRespCode(code.getValue());
        return this;
    }

    public VmMessageBuilder buildOrderSuccessList(List<VmMessageProto.VmMessage.OrderSuccess> osList) {
        builder.addAllOsList(osList);
        return this;
    }

    public String getVmNumber() {
        return this.vmNumber;
    }

    public VmMessageProto.VmMessage getMessage() {
        return this.builder.build();
    }


    public static void main(String[] args) {


        List<VmMessageProto.VmMessage.OrderSuccess> osList = new ArrayList<>();
        VmMessageProto.VmMessage.OrderSuccess.Builder b = VmMessageProto.VmMessage.OrderSuccess.newBuilder();
        b.setOrderNo("sadfd");
        b.setGoodsId(1);
        b.setGridNumber(23);
        b.setQty(1);
        osList.add(b.build());

        VmMessageProto.VmMessage message = new VmMessageBuilder("0755-01-04")
                .buildCmd(Cmd.HANDSHAKE_REQ)
                .buildRespCode(Code.DEFAULT)
                .buildOrderSuccessList(osList)
                .getMessage();

        System.out.println(message);
    }
}
